<script type="text/javascript">
	$(document).ready(function() {
		$('#data').dataTable({
			"bJQueryUI" : true,
			"aoColumnDefs" : [{
				"sType" : "formatted-num",
				"aTargets" : [2, 3]
			}]
		});
		$('.fg-toolbar').css('display', 'none');
	}); 
</script>
<style>
#detalle td{
	height: 30px;
	vertical-align: middle;
	font-weight: bold;
	padding-left: 10px;
	color: #365169;
}
.par td{
	background: #E2E4FF;
}
</style>
<table cellpadding="0" cellspacing="0" border="0"  id="detalle" style="width:540px">
	<tr class="par">
		<td>N&deg; Comprobante</td>
		<td><input disabled="true" type="text" value="<?php echo $venta['Venta']['nrocomprobante']; ?>"></td>
		<td>Fecha</td>
		<td><input type="text" disabled="true" value="<?php echo $venta['Venta']['fecha']; ?>"></td>
	</tr>
	<tr>
		<td>Tipo de Comprobante</td>
		<?php switch($venta['Venta']['tipocomprobante']){
				case 0:
					echo '<td><input disabled="true" type="text" value="Recibo"></td>';break;
				case 1:
					echo '<td><input disabled="true" type="text" value="Boleta"></td>';break;
				case 2:
					echo '<td><input disabled="true" type="text" value="Factura"></td>';
		} ?>
		<td>Tienda</td>
		<?php foreach ($tiendas as $t) { ?>
			<?php if($t['Tienda']['id']== $venta['Venta']['tiendas_id']) {?>
				<td><input disabled="true" type="text" value="<?php echo $t['Tienda']['nombre']; ?>"></td>
		<?php }} ?>
	</tr>
	<tr class="par">
		<td>Vendedor</td>
		<td>
			<?php
				$apellido = $this->Session->read('apellido');
				echo $apellido;
			?>
		</td>
		<td></td>
		<td></td>
	</tr>
	<tr>
		<td>Comprador</td>
		<td><input disabled="true" type="text" value="<?php echo $venta['Venta']['comprador'] ?>"></td>
		<td>RUC N&deg;</td>
		<td><input disabled="true" type="text" value="<?php echo $venta['Venta']['ruc'] ?>"  maxlength="11"></td>
	</tr>
</table>
<br />
<br />
<br />
<?php
	$submit = array(
		'label' => 'Agregar',
		'name' => 'data[accion]',
		'class' => 'but',
		'div' => false
	);
echo $this->Form->create('Venta', array('action' => 'agregarproducto' ,'type' => 'post')); ?>
<span class="titulo">Agregar producto</span>
<input type="hidden" name="data[Detalle][ventas_id]" value="<?php echo $venta['Venta']['id']?>"/>
<table cellpadding="0" cellspacing="0" border="0"  id="detalle" style="width:540px">
	<tr class="par">
		<td>Cantidad</td>
		<td>Cod. Producto</td>
		<td>Monto Total</td>
	</tr>
	<tr>
		<td><input name="data[Detalle][cantidad]" type="text" size="10px"/></td>
		<td><input name="data[Producto][codigo]" type="text" size="10px"/></td>
		<td><input name="data[Detalle][monto]" type="text" size="10px"/></td>
	</tr>
</table>
<div style="width:510px;text-align:right;"><?php echo $this->Form->end($submit); ?></div>
<?php $i=0; ?>
<table cellpadding="0" cellspacing="0" border="0" class="display" id="data" style="width:540px">
	<thead>
		<tr>
			<th>Cantidad</th>
			<th>Producto</th>
			<th>Precio unitario</th>
			<th>Monto</th>
			<th>Borrar</th>
		</tr>
	</thead>
	<tbody>
		<?php foreach ($detalles as $d) { 
			$i = $i + $d['Detalle']['monto'];?>
		<tr>
			<td style="vertical-align:middle;"><?php echo $d['Detalle']['cantidad'] ?></td>
			<td style="vertical-align:middle;">
				<?php 
				 foreach ($productos as $p) {
					if($p['Producto']['id'] == $d['Detalle']['productos_id']) {
				   	echo $p['Producto']['descripcion'];
				 }
				}?>
			</td>
			<td style="text-align:right; vertical-align:middle;">
				<?php 
				 foreach ($productos as $p) {
					if($p['Producto']['id'] == $d['Detalle']['productos_id']) {
				   	echo $p['Producto']['costo'];
				 }
				}?>
			</td>
			<td style="text-align:right; vertical-align:middle;"><?php echo $d['Detalle']['monto'] ?></td>
			<td>
				<a class="crud" style="margin-left:25px;" href="../Ventas/borrar_detalle?id=<?php echo $venta['Venta']['id']?>&idDetalle=<?php echo $d['Detalle']['id']; ?>">
					<img title="borrar" width="16px" height="16px" src="../css/images/borrar.png">
				</a>
			</td>
		</tr>
		<?php } ?>
		<tfoot>
			<tr>
				<td></td>
				<td></td>
				<td align="right"><b>Monto Total S/.</b></td>
				<td align="right"><b><?php echo $i; ?></b></td>
			</tr>
		</tfoot>
	</tbody>
</table>
<br /><br />
<?php
	$submit = array(
		'label' => 'Registrar Compra',
		'name' => 'data[accion]',
		'class' => 'but',
		'div' => false
	);
echo $this->Form->create('Venta', array('action' => 'guardarCompra' ,'type' => 'post')); ?>
<input type="hidden" name="data[Venta][id]" value="<?php echo $venta['Venta']['id']?>" />
<table cellpadding="0" cellspacing="0" border="0"  id="detalle" style="width:540px">
	<tr class="par">
		<td>Estado</td>
		<td>Adelanto</td>
	</tr>
	<tr>
		<td>
			<select name="data[Venta][estado]" style="width:153px;">	
				<option value="0">Pendiente</option>
				<option value="1">Pagado</option>
			</select>
		</td>
		<td><input name="data[Venta][adelanto]" type="text" size="10px"/></td>
	</tr>
</table>
<br />
<div style="width:510px;text-align:right;"><?php echo $this->Form->end($submit); ?></div>